local gaojie = fk.CreateSkill{
  name = "mini__gaojie",
}

Fk:loadTranslationTable{
  ["mini__gaojie"] = "高节",
  [":mini__gaojie"] = "出牌阶段开始时，你可以重铸你的区域内点数最大和最小的各一张牌，然后你可以令一名角色" ..
      "摸两张牌或回复1点体力且本回合你不能对体力值小于你的角色使用此点数区间内的牌。",

  ["#mini__gaojie-invoke"] = "是否发动 高节，重铸你的区域内的%arg和%arg2，<br/>然后你可以令一名角色" ..
      "摸两张牌或回复1点体力且本回合你不能使用点数%arg3~%arg4的牌",
  ["#mini__gaojie-recast_all_same"] = "是否发动 高节，重铸你的区域内的两张牌，<br/>然后你可以令一名角色" ..
      "摸两张牌或回复1点体力且本回合你不能使用此点数的牌",
  ["#mini__gaojie-recast_min"] = "是否发动 高节，重铸你的区域点数最大和最小的各一张牌，选择一张点数最小的牌",
  ["#mini__gaojie-recast_max"] = "是否发动 高节，重铸你的区域点数最大和最小的各一张牌，选择一张点数最大的牌",
  ["#mini__gaojie-choose"] = "高节：令一名角色摸两张牌或回复1点体力，然后本回合你不能使用此点数区间内的牌",
  ["mini__gaojie_draw2"] = "令%dest摸两张牌",
  ["mini__gaojie_recover"] = "令%dest回复1点体力",
  ["@mini__gaojie_prohibit-turn"] = "高节禁用",
}

gaojie:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and
      player.phase == Player.Play and #player:getCardIds("hej") > 1
  end,
  on_cost = function (self, event, target, player, data)
    local min, max = 14, 0
    local minCard, maxCard = {}, {} ---@type integer[], integer[]
    for _, c in ipairs(player:getCardIds("hej")) do
      local num = Fk:getCardById(c).number
      if num < min then
        min = num
        minCard = {c}
      elseif num == min then
        table.insert(minCard, c)
      end
      if num > max then
        max = num
        maxCard = {c}
      elseif num == max then
        table.insert(maxCard, c)
      end
    end
    local cards ---@type integer[]
    local room = player.room
    if #minCard == 1 and #maxCard == 1 then
      local minC, maxC = Fk:getCardById(minCard[1]), Fk:getCardById(maxCard[1])
      if room:askToSkillInvoke(player, {
        skill_name = self.name,
        prompt = "#mini__gaojie-invoke:::" .. minC:toLogString() .. ":" .. maxC:toLogString() ..
          ":" .. minC.number .. ":" .. maxC.number
      }) then
        cards = {minCard[1], maxCard[1]}
      end
    elseif min == max then
      cards = room:askToCards(player, {
        min_num = 2,
        max_num = 2,
        include_equip = true,
        skill_name = self.name,
        pattern = ".",
        cancelable = true,
        prompt = "#mini__gaojie-recast_all_same",
        expand_pile = player:getCardIds("j"),
      })
    else
      if #minCard > 1 then
        minCard = room:askToCards(player, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = self.name,
          pattern = tostring(Exppattern{ id = minCard }),
          cancelable = true,
          prompt = "#mini__gaojie-recast_min",
          expand_pile = player:getCardIds("j"),
        })
      end
      if #maxCard > 1 then
        maxCard = room:askToCards(player, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = self.name,
          pattern = tostring(Exppattern{ id = maxCard }),
          cancelable = true,
          prompt = "#mini__gaojie-recast_max",
          expand_pile = player:getCardIds("j"),
        })
      end
      if #minCard == 1 and #maxCard == 1 then
        cards = {minCard[1], maxCard[1]}
      end
    end
    if cards then
      event:setCostData(self, { cards = cards, nums = {min, max} })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local cards = event:getCostData(self).cards
    local room = player.room
    room:recastCard(cards, player, self.name)
    if player.dead then return end
    local targets = room:askToChoosePlayers(
      player,
      {
        targets = room.alive_players,
        min_num = 1,
        max_num = 1,
        prompt = "#mini__gaojie-choose",
        skill_name = self.name,
        cancelable = true,
      }
    )
    if #targets > 0 then
      target = targets[1]
      local choices = {"mini__gaojie_draw2::" .. target.id}
      if target:isWounded() then table.insert(choices, "mini__gaojie_recover::" .. target.id) end
      local choice = room:askToChoice(player, {
        choices = choices,
        skill_name = self.name
      })
      if choice:startsWith("mini__gaojie_draw2") then
        target:drawCards(2, self.name)
      else
        room:recover{ who = target, num = 1, recoverBy = player, skillName = self.name }
      end
      if player.dead then return end
      local nums = event:getCostData(self).nums
      room:setPlayerMark(player, "@mini__gaojie_prohibit-turn", nums[1] ~= nums[2] and nums[1] .. "~" .. nums[2] or nums[1])
      room:setPlayerMark(player, "_mini__gaojie_prohibit-turn", nums)
    end
  end
})

gaojie:addEffect("prohibit", {
  is_prohibited = function (self, player, to, card)
    return player:getMark("_mini__gaojie_prohibit-turn") ~= 0 and
      card.number >= player:getMark("_mini__gaojie_prohibit-turn")[1] and
      card.number <= player:getMark("_mini__gaojie_prohibit-turn")[2] and
      to.hp < player.hp
  end
})

return gaojie
